<?php

namespace App\Http\Controllers\Main;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Article;

class NewsController extends Controller
{
    public function index()
    {

        #获取一个
        $newsOne = Article::orderBy('rank','DESC')->orderBy('create_time','DESC')->first();
        #获取列表内容
//        if($page){
//            $news = Article::whereNotIn('id',[$newsOne->id])->orderBy('rank','DESC')->orderBy('create_time','DESC')->skip(($page-1)*6)->paginate(6);
//        }else{
//            $news = Article::whereNotIn('id',[$newsOne->id])->orderBy('rank','DESC')->orderBy('create_time','DESC')->paginate(6);
//        }
        $news = Article::whereNotIn('id',[$newsOne->id])->orderBy('rank','DESC')->orderBy('create_time','DESC')->paginate(10);
        $color = 5;
        return view('main/news', compact('news','newsOne','color'));
    }

    public function detail($id)
    {
        // 更新view字段，累计查看次数
        Article::where('id', $id)->update([
            'view' => Article::whereId($id)->pluck('view')[0] + 1
        ]);
        #新闻详情
        $data = Article::where('id', $id)->first();
        $data->content = str_replace('<img src="/','<img src="http://www.vipyipoint.com/',$data->content);
        #更多新闻
        $more = Article::orderBy('rank','DESC')->orderBy('create_time','DESC')->take(5)->get();
        #获取左栏数据
        $productCate = $this->productCate();
        $color = 5;
        return view('main/news/detail', compact('data','more','color','productCate'));
    }
}
